import Vue from "vue";
import Router from "vue-router";
import { getUser } from "@/api/user";
Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/",
    redirect: "/transfer",
    hidden: true,
  },
  {
    path: "/transfer",
    name: "Transfer",
    component: () => import("@/views/TransferPage/index"),
  },

  {
    path: "/data-entry",
    component: Layout,
    meta: {
      title: "数据录入",
      icon: "el-icon-s-check",

      // userRole: [
      //   "test",
      //   "kbslr",
      //   "dslr",
      //   "dltqlr",
      //   "zgeqlr",
      //   "etkqqlr",
      //   "etkqlr",
      //   "hjqlr",
      //   "wsqlr",
      // ],
    },

    children: [
      {
        path: "",
        name: "DataEntry",
        component: () => import("@/views/DataEntry/index.vue"),
        meta: { title: "数据录入", icon: "el-icon-s-check" },
      },
      {
        path: "new",
        name: "NewData",
        hidden: true,
        component: () => import("@/views/DataEntry/NewData.vue"),
      },
    ],
  },
  {
    path: "/data-audit",
    component: Layout,
    meta: {
      title: "数据审核",
      icon: "el-icon-s-check",
    },
    children: [
      {
        path: "",
        name: "DataAudit",
        component: () => import("@/views/DataAudit/index.vue"),
        meta: { title: "数据审核", icon: "el-icon-s-check" },
      },
      // {
      //   path: "details/:id",
      //   name: "AuditDetails",
      //   hidden: true,
      //   component: () => import("@/views/DataAudit/AuditDetails.vue"),
      //   meta: { title: "审核详情" },
      // },
    ],
  },

  {
    path: "/interface-management",
    component: Layout,

    meta: { title: "断面管理", icon: "el-icon-s-data" },
    children: [
      {
        path: "",
        name: "InterfaceManagement",
        component: () => import("@/views/InterfaceManagement/index.vue"),
        meta: { title: "断面管理", icon: "el-icon-s-data" },
      },
      {
        path: "InterfaceManagementAdd/:data?", // 这里使用动态路由参数 id
        name: "InterfaceManagementAdd",
        hidden: true, // 在侧边栏中隐藏该项
        component: () => import("@/views/InterfaceManagement/Add/index.vue"),
      },
    ],
  },
  {
    path: "/statistics",
    component: Layout,

    redirect: "/statistics/index",
    meta: { title: "统计", icon: "el-icon-s-data" },
    children: [
      {
        path: "index",
        component:() => import("@/views/Statistics/index.vue"),
        meta: { title: "统计", icon: "el-icon-s-data" }, // 假设 icon 是一个图标的类名
      }]
  },


  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

// const router = new Router({
//   // mode: 'history', // require service support
//   scrollBehavior: () => ({ y: 0 }),
//   routes: constantRoutes,
// });

const router = createRouter();
// let userInfo = null;
// router.beforeEach(async (to, from, next) => {
//   if (userInfo) {
//     // 如果已经获取过，直接使用缓存的用户信息
//     // 其余逻辑...
//     next();
//   } else {
//     // 如果没有获取过，发起请求获取用户信息
//     try {
//       // const response = await getUser();
//       const response = await Vue.prototype.$api.getUser();

//       // if (response.data.code === 906) {
//       //   // token已过期，重定向到登录页面
//       //   return next("/login");
//       // }
//       userInfo = response.data.data;
//       // 从接口返回的数据中获取用户的areacode
//       const areacode = response.data.data.areacode;

//       // 根据areacode来判断用户角色
//       let userRole;

//       // 检查用户角色是否具有访问数据录入模块的权限
//       // 检查用户角色是否具有访问数据录入模块的权限
//       // if (
//       //   userRole &&
//       //   constantRoutes.find((route) => route.path === "/data-entry")
//       // ) {
//       //   // 用户具有权限，并且数据录入模块存在，继续向下执行
//       //   next();
//       // } else {
//       //   // 用户没有权限或者数据录入模块不存在，重定向到其他页面
//       //   next("/interface-management");
//       // }
//     } catch (error) {
//       // 处理获取用户角色信息失败的情况
//       console.error("Failed to fetch user role:", error);
//       // 可以选择重定向到登录页面或其他适当的处理方式
//       next("/login");
//     }
//   }
//   // 获取用户角色信息
// });

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
